exports.install = function (Vue, options) {
    Vue.prototype.successToShow = function (msg,callback) {
        uni.showToast({
            title: msg,
			icon:'none',
            duration: 1500
        })
        if(typeof callback == 'function'){
            setTimeout(function () {
                callback()
            }, 1500)
        }
       
    };
    Vue.prototype.errorToShow = function (msg,callback) {
        uni.showToast({
            title: msg,
            icon: 'none',
            duration: 1500
        })
        if(typeof callback == 'function'){
            setTimeout(function () {
                callback()
            }, 1000)
        }
        
    };
	Vue.prototype.showModelTip = function (content,callback,showCancel=false){
		uni.showModal({
			title:'系统提示',
			content,
			confirmText:'确认',
			showCancel,
			success:(res) => {
				if(res.confirm){
					if(typeof callback == 'function'){
						callback()
					}
				}
			}
		})
	};
    Vue.prototype.redirect = function (url) {
        uni.redirectTo({
            url
        });
    };
    Vue.prototype.href = function (url) {
        uni.navigateTo({
            url
        });
    };
    Vue.prototype.launch = function (url) {
        uni.reLaunch({
            url
        });
    };
    // 转换时间格式
    Vue.prototype.transformTime = function (time) {
        var date = new Date(time * 1000);
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = date.getDate() + ' ';
        return Y + M + D
    };
	Vue.prototype.getDateYMDHMS = function (time) {
		// 公用模块用法请参考 https://uniapp.dcloud.io/uniCloud/cf-common
		const format = '{y}/{m}/{d} {h}:{i}:{s}'
		var date = new Date(time * 1000);
		const formatObj = {
			y:date.getFullYear(),
			m:date.getMonth() + 1,
			d:date.getDate(),
			h:date.getHours(),
			i:date.getMinutes(),
			s:date.getSeconds()
		}
		const strTime = format.replace(/{(y|m|d|h|i|s)}/g,(result,key)=>{
			let value = formatObj[key]
			if(value>=0&&value<10){
				value = '0' + value
			}
			return value
		})
		return strTime
	}
	Vue.prototype.getDateYMD = function (time) {
		// 公用模块用法请参考 https://uniapp.dcloud.io/uniCloud/cf-common
		const format = '{y}/{m}/{d} {h}:{i}:{s}'
		var date = new Date(time * 1000);
		const formatObj = {
			y:date.getFullYear(),
			m:date.getMonth() + 1,
			d:date.getDate()
		}
		const strTime = format.replace(/{(y|m|d)}/g,(result,key)=>{
			let value = formatObj[key]
			if(value>=0&&value<10){
				value = '0' + value
			}
			return value
		})
		return strTime
	}
    //时间转刚刚，昨天，
    Vue.prototype.timeToCn= function( timestamp ) {
        function zeroize( num ) {
            return (String(num).length == 1 ? '0' : '') + num;
        }
        var curTimestamp = parseInt(new Date().getTime() / 1000); //当前时间戳
        var timestampDiff = curTimestamp - timestamp; // 参数时间戳与当前时间戳相差秒数
    
        var curDate = new Date( curTimestamp * 1000 ); // 当前时间日期对象
        var tmDate = new Date( timestamp * 1000 );  // 参数时间戳转换成的日期对象
    
        var Y = tmDate.getFullYear(), m = tmDate.getMonth() + 1, d = tmDate.getDate();
        var H = tmDate.getHours(), i = tmDate.getMinutes(), s = tmDate.getSeconds();
    
        if ( timestampDiff < 60 ) { // 一分钟以内
            return "刚刚";
        } else if( timestampDiff < 3600 ) { // 一小时前之内
            return Math.floor( timestampDiff / 60 ) + "分钟前";
        } else if ( curDate.getFullYear() == Y && curDate.getMonth()+1 == m && curDate.getDate() == d ) {
            return '今天' + zeroize(H) + ':' + zeroize(i);
        } else {
            var newDate = new Date( (curTimestamp - 86400) * 1000 ); // 参数中的时间戳加一天转换成的日期对象
            if ( newDate.getFullYear() == Y && newDate.getMonth()+1 == m && newDate.getDate() == d ) {
                return '昨天' + zeroize(H) + ':' + zeroize(i);
            } else if ( curDate.getFullYear() == Y ) {
                return  zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i);
            } else {
                return  Y + '年' + zeroize(m) + '月' + zeroize(d) + '日 ' + zeroize(H) + ':' + zeroize(i);
            }
        }
    };
    Vue.prototype.formatRichText = function (html) {
        // 去掉img标签里的style、width、height属性
		let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
			match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
			match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
			match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
			return match;
		});
		// 修改所有style里的width属性为max-width:100%
		newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
			match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
			return match;
		});
		// 去掉<br/>标签
		newContent = newContent.replace(/<br[^>]*\/>/gi, '');
		// img标签添加style属性：max-width:100%;height:auto
		newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:0px auto;"').replace(/<o:p>/g, "").replace(/<\/o:p>/g, "");
		return newContent;
    };
    	//获取当前页面地址
    Vue.prototype.getPageUrl = function () {
        let pages = getCurrentPages();
		let curPage = pages[pages.length-1];
		if(curPage){
			let url = '/'+curPage.route
			let options = curPage.options || curPage.$route.query;
			var page = url + '?'
			for (let key in options) {
				var value = options[key]
				page += key + '=' + value + '&'
			}
			page = page.substring(0, page.length - 1)
			return page;
		}else{
			return '/'
		}
    };
	Vue.prototype.getUrlParam = function (name) {
	    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	    var r = window.location.search.substr(1).match(reg);
	    if (r != null) return unescape(r[2]);
	    return null;
	};
    //获取url参数
    Vue.prototype.getQueryVariable = function (variable) {
	   var query = window.location.search.substring(1);
	   var vars = query.split("&");
	   for (var i=0;i<vars.length;i++) {
			   var pair = vars[i].split("=");
			   if(pair[0] == variable){return pair[1];}
	   }
	   return(false);
    };
     //获取url参数
     Vue.prototype.zwshare = function (title,content,image,url) {
        let {appname} = require('@/common/config');
        // console.log(appname);
        ZWJSBridge.share({
                'title':title?title:appname,
                'content':content?content:'',
                'image':image?image:'',
                'url':url?url:'',
            }).then((result) => {
                console.log(result);
            }).catch((error) => {
                console.log(error);
            });
            
    };
    Vue.prototype.getDate = function(type){
        const date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();

        if (type === 'start') {
            year = year - 60;
        } else if (type === 'end') {
            year = year + 2;
        }
        month = month > 9 ? month : '0' + month;
        day = day > 9 ? day : '0' + day;
        return `${year}-${month}-${day}`;
    }
	Vue.prototype.hasNull = function(params) {
		var keys = Object.keys(params);
		for (var i = 0; i < keys.length; i++) {
			var obj = params[keys[i]];
			// console.log(keys[i],obj,obj == "")
			if(obj===0){
				return false;
			}
			if (obj == null || obj === "" || obj == undefined) {
				return true;
			}
		}
		return false;
	}
};


